// pages/artificer/income_data/income_data.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    tab:[
      {name:'今天',selected:true},
      {name:'昨天',selected:false},
      {name:'本月内',selected:false},
      {name:'总的',selected:false},
      {name:'自定义',selected:false},
    ],
    timeIndex:0,
    timeBox: false,
    star_time:'',
    end_time:'',
    tab2:[
      {name:'收入',selected:true},
      {name:'支出',selected:false},
    ],
    tab2Index:0,
    moneys:0,
    orderCount:0,
    page:1,
    time:0,  //0=今天,1=昨天,2=本月内,3=总的, [Y-m-d - Y-m-d]=自定义  
    lists:[],
    page:1,
    lastPage:0,
    type:2 //类型:0=代理商,1=商家,2=技师师傅  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.managerServices();
    
  },
  bindDateChange(e){
    let key = e.currentTarget.dataset.key;
    let val = e.detail.value;
    this.setData({
      [key]:val
    })
  },
  changeTopTab(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      tab2Index:index,
      lists:[],
      page:1,
      lastPage:0
    })
    if(index == 0){
      this.managerServices();
    }else if(index == 1){
      this.managerEexpends();
    }
  },
  commitTimeBox(){
    this.setData({
      time:this.data.star_time+" - "+this.data.end_time
    })
    this.managerServices()
    this.closeTimeBox();
  },
  closeTimeBox(){
    this.setData({
      timeBox:false
    })
  },
  getWork(){
    api.get('/manager/worker').then(res=>{
      this.setData({
        info:res.detail
      })
    })
  },
  //获取统计数据

  managerServices(){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/manager/services',{
      type:this.data.type,
      time:this.data.time,
      page:this.data.page
    }).then(res=>{
      this.setData({
        moneys:res.moneys,
        orderCount:res.order_count,
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
    })
  },
  //获取支出记录
  managerEexpends(){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/manager/expends',{
      type:this.data.type,
      time:this.data.time,
      page:this.data.page
    }).then(res=>{
      this.setData({
        moneys:res.moneys,
        orderCount:res.order_count,
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
    })
  },
  selectTimeTab(e){
    let index = e.currentTarget.dataset.index;
    if(index == 4){ //自定义
      this.setData({
        timeBox:true
      })
      return false;
    }
    this.setData({
      timeIndex:index,
      tab2Index:0,
      time:index,
      page:1,
      lastPage:0,
      lists:[]
    })
    this.managerServices();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
    let menu = this.selectComponent("#menu");
    menu.setMenu()
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWork();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})